<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  <title>
    spring swagger | king019
  </title>
  <meta name="description" content>
  
  <meta name="keywords" content="
  swagger
  ">
  
  <meta name="author" content="king019">

  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="theme-color" content="#1e2327">
  <link rel="apple-touch-icon" href="https://github.githubassets.com/apple-touch-icon.png">
  <link rel="apple-touch-icon" sizes="180x180" href="https://github.githubassets.com/apple-touch-icon-180x180.png">

  <link rel="icon" type="image/x-icon" href="https://github.githubassets.com/favicon.ico">
  <link rel="stylesheet" href="/css/main.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  

  

  <script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.25-csp/vue.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
</head>

<body id="replica-app">

<nav class="navbar-wrapper">
  <div class="navbar">
    <div class="container clearfix">
      <a href="/" class="navbar-logo"><i class="fa fa-github"></i></a>

      <div class="navbar-search float-left desktop-only">
        <div class="navbar-search-form">
          <label for="gsc-i-id1">This website</label>
          <div id="google-search">
            <gcse:search></gcse:search>
          </div>
        </div>
      </div>

      <ul class="navbar-nav float-left">
        
        <li><a href="/archives">Archives</a></li>
        
        
        <li><a href="/categories">Categories</a></li>
        
        
        <li><a href="/tags">Tags</a></li>
        
        
        <li class="desktop-only"><a href="/atom.xml" target="_blank">RSS</a></li>
        
      </ul>

      <ul class="navbar-nav user-nav float-right desktop-only">
        <li class="user-nav-notification">
          <a><span class="user-nav-unread"></span><i class="fa fa-bell"></i></a>
        </li>
        <li>
          <a><i class="fa fa-plus"></i> <i class="fa fa-caret-down"></i></a>
        </li>
        <li class="user-nav-logo">
          <a><img src="/assets/head.jpg"> <i class="fa fa-caret-down"></i></i></a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<div class="main-container">
  <header class="header-wrapper desktop-only">
  <div class="container header-site-detail">
    <ul class="header-toolbar">
      <li class="clearfix">
        <a href="/archives" class="header-toolbar-left"><i
                  class="fa fa-file-text"></i> Posts </a>
        <a href="/archives"
           class="header-toolbar-right"> 67 </a>
      </li>
      <li>
        <a href="/tags" class="header-toolbar-left"><i
                  class="fa fa-tags"></i> Tags </a>
        <a href="/tags"
           class="header-toolbar-right"> 53 </a>
      </li>
      <li>
        <a href="/categories" class="header-toolbar-left"><i
                  class="fa fa-folder-open"></i> Categories </a>
        <a href="/categories"
           class="header-toolbar-right"> 20 </a>
      </li>
    </ul>
    <h2 class="header-title">
      <i class="fa fa-book text-muted"></i>
      <a href="/">king019</a>
      
      
    </h2>
  </div>

  <div class="container">
    <div class="header-tab-wrapper clearfix">
      <span class="header-tab header-tab-selected"><i class="fa fa-thumbs-o-up"></i> Like</span>
      <span class="header-tab"><i class="fa fa-share-alt"></i> Share</span>
      <span class="header-tab"><i class="fa fa-comments-o"></i> Discussion</span>
      <span class="header-tab"><i class="fa fa-bookmark-o"></i> Bookmark </span>
      <span class="header-tab"><i class="fa fa-smile-o"></i> Smile <i class="fa fa-caret-down"></i></span>
    </div>
  </div>
</header>


<div class="post-container container">
  <h3>
    <i class="fa fa-user-o"></i>
    king019

    <span class="post-date float-right" title="{{moment(1615129899338).format('MMM DD, YYYY, h:mm:ss A')}}">
      
          <i class="fa fa-pencil-square-o"></i>
      
      {{moment(1615129899338).fromNow()}}
    </span>
  </h3>

  <article class="post-content">
    <h1>spring swagger</h1>
    <p>一,api注解解析<br>1,注解解析入口</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">springfox.documentation.spring.web.readers.operation.ApiOperationReader#read</span><br></pre></td></tr></table></figure>

<p>2,遍历注解,开始生成参数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apiListingScanner.scan(listingContext);</span><br></pre></td></tr></table></figure>

<p>2.1生成swagger组,然后遍历组下参数注解</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;ResourceGroup, List&lt;RequestMappingContext&gt;&gt; requestMappingsByResourceGroup= context.getRequestMappingsByResourceGroup();</span><br></pre></td></tr></table></figure>

<p>2.2按照组获取链接,并进行遍历</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List&lt;RequestMappingContext&gt; requestMappings = nullToEmptyList(requestMappingsByResourceGroup.get(resourceGroup));</span><br></pre></td></tr></table></figure>

<p>2.3遍历apiModel对象</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">models.putAll(apiModelReader.read(each.withKnownModels(models)));</span><br></pre></td></tr></table></figure>

<p>2.4遍历apiDescription对象</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apiDescriptions.addAll(apiDescriptionReader.read(each));</span><br></pre></td></tr></table></figure>

<p>2.4.1</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">springfox.documentation.spring.web.scanners.ApiDescriptionReader#read</span><br></pre></td></tr></table></figure>

<p>2.4.1.1选取方法路径,进行遍历(使用google缓存进行内存保存)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Operation&gt; operations = operationReader.read(operationContext);</span><br></pre></td></tr></table></figure>

<p>2.4.1.1.1按照支持的方法,遍历操作注解</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">for (RequestMethod httpRequestMethod : supportedMethods)</span><br></pre></td></tr></table></figure>

<p>2.4.1.1.2遍历操作注解</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Operation operation = pluginsManager.operation(operationContext);</span><br><span class="line">springfox.documentation.spring.web.plugins.DocumentationPluginsManager#operation</span><br><span class="line">  public Operation operation(OperationContext operationContext) &#123;</span><br><span class="line">    for (OperationBuilderPlugin each : operationBuilderPlugins.getPluginsFor(operationContext.getDocumentationType())) &#123;</span><br><span class="line">      each.apply(operationContext);</span><br><span class="line">    &#125;</span><br><span class="line">    return operationContext.operationBuilder().build();</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>2.4.1.1.2.1在遍历后,将参数添加到上下文中,并且对于相同名称的参数进行合并</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">public OperationBuilder parameters(final List&lt;Parameter&gt; parameters) &#123;</span><br><span class="line">  List&lt;Parameter&gt; source = nullToEmptyList(parameters);</span><br><span class="line">  List&lt;Parameter&gt; destination = newArrayList(this.parameters);</span><br><span class="line">  ParameterMerger merger = new ParameterMerger(destination, source);</span><br><span class="line">  this.parameters = newArrayList(merger.merged());</span><br><span class="line">  return this;</span><br><span class="line">&#125;</span><br><span class="line">public List&lt;Parameter&gt; merged() &#123;</span><br><span class="line">  Set&lt;String&gt; existingParameterNames = from(destination).transform(toParameterName()).toSet();</span><br><span class="line">  Set&lt;String&gt; newParameterNames = from(source).transform(toParameterName()).toSet();</span><br><span class="line">  List&lt;Parameter&gt; merged = newArrayList();</span><br><span class="line"></span><br><span class="line">  SetView&lt;String&gt; asIsParams = difference(existingParameterNames, newParameterNames);</span><br><span class="line">  SetView&lt;String&gt; missingParamNames = difference(newParameterNames, existingParameterNames);</span><br><span class="line">  SetView&lt;String&gt; paramsToMerge = Sets.intersection(newParameterNames, existingParameterNames);</span><br><span class="line"></span><br><span class="line">  merged.addAll(asIsParameters(asIsParams, destination));</span><br><span class="line">  merged.addAll(newParameters(missingParamNames, source));</span><br><span class="line">  merged.addAll(mergedParameters(paramsToMerge, destination, source));</span><br><span class="line">  return merged;</span><br><span class="line">&#125;</span><br><span class="line">private List&lt;Parameter&gt; mergedParameters(</span><br><span class="line">    SetView&lt;String&gt; paramsToMerge,</span><br><span class="line">    List&lt;Parameter&gt; existingParameters,</span><br><span class="line">    List&lt;Parameter&gt; newParams) &#123;</span><br><span class="line">  List&lt;Parameter&gt; parameters = newArrayList();</span><br><span class="line">  for (Parameter newParam : newParams) &#123;</span><br><span class="line">    Optional&lt;Parameter&gt; original = from(existingParameters).firstMatch(withName(newParam.getName()));</span><br><span class="line">    if (paramsToMerge.contains(newParam.getName()) &amp;&amp; original.isPresent()) &#123;</span><br><span class="line">      if (newParam.getOrder() &gt; original.get().getOrder())&#123;</span><br><span class="line">        parameters.add(merged(newParam, original.get()));</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        parameters.add(merged(original.get(), newParam));</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  return parameters;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>二,api数据获取</p>
<p>获取/v2/api-docs数据</p>
<p>springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation</p>

  </article>
</div>


    




</div>

<div class="footer-wrapper container">
  <footer class="footer clearfix">
    <div class="clearfix">
    <a href="https://king019.github.io" class="footer-logo">
      <i class="fa fa-github"></i>
    </a>
    <ul class="footer-social-link">
      <li>© 2019 king019</li>
      <li><a href="https://king019.github.io">Home</a></li>
      
      <li><a href="https://github.com/king019">Github</a></li>
      
    </ul>
    <div class="footer-theme-info">
      Theme <a href="//github.com/sabrinaluo/hexo-theme-replica">Replica</a>
      by <a href="//github.com/sabrinaluo">Hiitea</a> ❤ Powered by Hexo
    </div>
    </div>
    
  </footer>
</div>




<script src="/js/main.js"></script>

</body>
</html>
